<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>集合封装</title>
</head>
<body>
<script>
    function Set() {

        this.items = {};

        Set.prototype.add = function (value) {
            if(this.has(value)) {
                return false;
            }
            this.items[value] = value;
            return true;
        };

        Set.prototype.has = function (value) {
            return this.items.hasOwnProperty(value)
        };

        Set.prototype.remove = function (value) {
            if(!this.has(value)) {
                return false;
            }
            delete this.items[value];
            return true;
        };

        Set.prototype.clear = function () {
            this.items = {}
        };

        Set.prototype.size = function () {
            return Object.keys(this.items).length;
        };

        Set.prototype.values = function () {
            return Object.keys(this.items);
        };

        //两个Set的并集
        Set.prototype.union = function (otherSet) {
            let unionSet = new Set();

            let values = this.values();
            for (let i of values) {
                unionSet.add(i);
            }

            values = otherSet.values();
            for (i of values) {
                unionSet.add(i);
            }

            return unionSet;
        };

        //两个Set的交集
        Set.prototype.intersection = function (otherSet) {
            let intersectionSet = new Set();

            let values = this.values();

            for (let i of values) {
                if(otherSet.has(i)) {
                    intersectionSet.add(i);
                }
            }
            return intersectionSet;
        };

        //两个Set的差集
        Set.prototype.difference = function (otherSet) {
            let differenceSet= new Set();
            let values = this.values();
            for(let i of values) {
                if(!otherSet.has(i)) {
                    differenceSet.add(i)
                }
            }
            return differenceSet;
        };

        //判断传入otherSet是否是子集
        Set.prototype.subset = function (otherSet) {
            let values = otherSet.values();
            for(let i of values) {
                if(!this.has(i)) {
                    return false;
                }
            }
            return true;
        }
    }
    let setA = new Set();
    setA.add("三笠");
    setA.add("鲁路修");
    setA.add("马修");

    let setB = new Set();
    setB.add("鲁路修");
    setB.add("康奈");
    setB.add("CC");

    let setC = new Set();
    setB.add("CC");

    let ss = setA.union(setB);
    let ss2 = setA.intersection(setB);
    console.log(ss.values());
    console.log(ss2.values());
    console.log(setA.difference(setB).values());
    console.log(setB.subset(setC))
    
</script>
</body>
</html>